package com.hmall.item.controller;

import com.hmall.item.pojo.Item;
import com.hmall.item.service.ItemService;
import com.hmall.item.util.PageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @GetMapping("/list")
    public PageDTO<Item> getItems(@RequestParam("page") Integer page,
                                  @RequestParam("size") Integer size) {
        return itemService.getItems(page, size);
    }

    @GetMapping("{id}")
    public Item getItemById(@PathVariable("id") Long id) {
        return itemService.getItemsById(id);
    }

    @PostMapping
    public void insertItem(@RequestBody Item item) {
        itemService.insertItem(item);
    }

    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        itemService.updateStatus(id, status);
    }

    @PutMapping
    public void updateItem(@RequestBody Item item) {
        itemService.updateItem(item);
    }

    @DeleteMapping("{id}")
    public void deleteItem(@PathVariable Long id) {
        itemService.deleteItem(id);
    }

    @PutMapping("/stock/{itemId}/{num}")
    Integer modifyStock(@PathVariable("itemId") Long itemId, @PathVariable("num") Integer num){
        return itemService.modifyStock(itemId,num);
    }
}
